package org.colafries.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.colafries.admin.entity.vo.ColumnRspVO;
import org.colafries.admin.entity.vo.CustomTableReqVO;
import org.colafries.admin.entity.vo.QueryDataReqVO;
import org.colafries.admin.service.IDataBaseService;
import org.colafries.admin.entity.domain.CustomTable;
import org.colafries.admin.mapper.CustomTableMapper;
import org.colafries.admin.service.ICustomTableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author colafries
 * @version v1.0
 * @ClassName ICustomTableServiceImpl
 * @Description 自定义表Service
 * @date Created in 2024/4/8
 **/
@Service
@Slf4j
public class ICustomTableServiceImpl extends ServiceImpl<CustomTableMapper, CustomTable> implements ICustomTableService {
    @Resource
    IDataBaseService iDataBaseService;

    /**
     * 获取自定义表列表
     *
     * @param customTableReqVO
     * @return java.util.List<org..colafries.admin.entity.domain.CustomTable>
     * @author colafries
     * @Date 2024/4/8
     */
    @Override
    public List<CustomTable> getAll(CustomTableReqVO customTableReqVO) {
        return this.query().list();
    }

    @Override
    public List<ColumnRspVO> getTableSchema(CustomTableReqVO customTableReqVO) {
        Integer tableId = customTableReqVO.getTableId();
        String tableName = customTableReqVO.getTableName();

        CustomTable customTable = this.lambdaQuery()
                .eq(CustomTable::getId, tableId)
                .or()
                .eq(CustomTable::getName, tableName)
                .one();
        if (null == customTable) {
            log.info("自定义表不存在，id " + tableId + ",表名 " + tableName);
            return null;
        }

        return iDataBaseService.querySqlSchema(customTableReqVO.getDataSourceId(), customTable.getCustomSql());
    }

}
